package com.weiaibenpao.demo.chislim.ui.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.bean.SportHistoryBean;
import com.weiaibenpao.demo.chislim.ui.activity.running.ShareActivity;
import com.weiaibenpao.demo.chislim.utils.RunningUtils;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by lenovo on 2018/1/8.
 */

public class SportHistoryDateAdapter extends RecyclerView.Adapter<SportHistoryDateAdapter.ViewHolder> {
    private LayoutInflater mInflater;
    private ArrayList<SportHistoryBean.ListBean.SportsBean> sportMonthList;
    private DecimalFormat mDecimalFormat = new DecimalFormat("0.00");
    Context context;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd  HH:mm");//这个是你要转成后的时间的格式
    private static OnItemClickListener mOnItemClickListener;
    //设置点击和长按事件

    public interface OnItemClickListener {
        void setOnItemClick(View view, String h5_url); //设置点击事件
    }

    public static void setOnItemClickListener(OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    /**
     * 构造器
     * @param context
     */
    public SportHistoryDateAdapter(Context context) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);

    }

    /**
     * item显示类型
     * 引入布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.djq_sporthistory_date_item, parent, false);
        //view.setBackgroundColor(Color.RED);
        TextView textView = view.findViewById(R.id.disText);
        Typeface typeFace = Typeface.createFromAsset(parent.getContext().getAssets(), "fonts/Reducto Condensed SSi Condensed.ttf");
        textView.setTypeface(typeFace);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 数据的绑定显示
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        //绑定事件
        holder.dateTitle.setText(sdf.format(new Date(Long.parseLong(String.valueOf(( sportMonthList.get(position)).getSportDate())))) +" " + initSportText((sportMonthList.get(position)).getSportType()));
        holder.disText.setText(mDecimalFormat.format(( sportMonthList.get(position)).getTotalDistance()));
//        holder.sportInfo.setText(initTime(( sportMonthList.get(position)).getTotalTime()));
        holder.sportInfo.setText(RunningUtils.getHmsTime(( sportMonthList.get(position)).getTotalTime()));
        Logger.e("ljb: 里层布局");
    }

    public String initTime(int second){
        int hour = second/3600;
        int minute = (second - hour*3600)/60;
        second = second-hour*300-minute*60;
        return hour + ":" + minute + ":" + second;
    }

    public String initSportText(int n){
        if(n == 0){
            return "常规·室内";
        }else if(n == 1){
            return "常规·室外";
        }else if(n == 2){
            return "行政·室内";
        }else if(n == 3){
            return "行政·室外";
        }else if(n == 4){
            return "趣味·室内";
        }else if(n == 5){
            return "趣味·室外";
        }
        return "";
    }

    //刷新
    public void setData(ArrayList<SportHistoryBean.ListBean.SportsBean> dataList){
        sportMonthList = new ArrayList<>();
        sportMonthList = dataList;
        notifyDataSetChanged();
    }

    //加载更多
    public void initAdd(ArrayList<SportHistoryBean.ListBean.SportsBean> dataList){
        sportMonthList.addAll(dataList);
        notifyDataSetChanged();
    }

    /**
     * 要显示的item数目
     * @return
     */
    @Override
    public int getItemCount() {
        return sportMonthList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView sportInfo;
        public TextView dateTitle;
        public TextView disText;

        public ViewHolder(View view) {
            super(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SportHistoryBean.ListBean.SportsBean bean = sportMonthList.get(getAdapterPosition());

                    ShareActivity.startSelf(v.getContext(),bean.getDetailUrl(),initSportText(bean.getSportType()));
                }
            });
            sportInfo = (TextView) view.findViewById(R.id.sportInfo);
            dateTitle = (TextView) view.findViewById(R.id.dateTitle);
            disText = (TextView) view.findViewById(R.id.disText);
        }
    }
}
